<?php
// +----------------------------------------------------------------------
// | SwiftPHP [ JUST DO ONE THING WELL ]
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2017  http://swiftphp.zhongwenyu.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: zhongwenyu <zhongwenyu1987@163.com> <http://www.zhongwenyu.com>
// +----------------------------------------------------------------------
// | Times: 2017/2/16 16:12
// +----------------------------------------------------------------------
namespace swift;
use swift\response\Json as JsonResponse;
use swift\response\Redirect as RedirectResponse;
use swift\response\View as ViewResponse;
use swift\response\Xml as XmlResponse;

class Response{
    // 原始数据
    protected $data;
    // 当前的contentType
    protected $contentType = 'text/html';
    // 字符集
    protected $charset = 'utf-8';
    //状态码
    protected $code = 200;
    // 输出参数
    protected $options = [];
    // header参数
    protected $header = [];
    // 输出内容
    protected $content = null;

    /**
     * 架构函数
     * @access   public
     * @param mixed $data    输出数据
     * @param int   $code
     * @param array $header
     * @param array $options 输出参数
     */
    public function __construct($data = '', $code = 200, array $header = [], $options = []){
        $this->data = $data;
        $this->header = $header;
        $this->code = $code;
        if (!empty($options)) {
            $this->options = array_merge($this->options, $options);
        }
        $this->contentType($this->contentType, $this->charset);
    }

    /**
     * 设置页面输出类型
     * @param string $contentType 输出类型
     * @param string $charset     输出编码
     * @return $this
     */
    public function contentType($contentType, $charset = 'utf-8'){
        $this->header['Content-Type'] = $contentType . ';charset=' . $charset;
        return $this;
    }

    /**
     * 创建Response对象
     * @access public
     * @param mixed  $data    输出数据
     * @param string $type    输出类型：json，xml，view，redirect
     * @param int    $code
     * @param array  $header
     * @param array  $options 输出参数
     * @return Response|JsonResponse|ViewResponse|XmlResponse|RedirectResponse
     */
    public static function create($data = '', $type = '', $code = 200, array $header = [], $options = []){
        empty($type) && $type = null;
        $class = false !== strpos($type, '\\') ? $type : '\\swift\\response\\' . ucfirst(strtolower($type));
        if (class_exists($class)) {
            $response = new $class($data, $code, $header, $options);
        } else {
            $response = new static($data, $code, $header, $options);
        }
        return $response;
    }

    /**
     * 发送数据到客户端
     * @access public
     * @return mixed
     * @throws \InvalidArgumentException
     */
    public function send(){
        // 处理输出数据
        $data = $this->getContent();

        if (200 == $this->code) {
            // 缓存检查
        }
        
        if(!headers_sent() && !empty($this->header)){
            // 发送状态码
            http_response_code($this->code);
            // 发送头部信息
            foreach($this->header as $name => $value){
                header($name . ':' . $value);
            }
        }
        // 输出数据
        echo $data;
        if (function_exists('fastcgi_finish_request')) {
            // 提高页面响应
            fastcgi_finish_request();
        }
    }

    /**
     * 获取输出数据
     * @return mixed
     */
    public function getContent(){
        if(is_null($this->content)){
            $content = $this->output($this->data);
            if(null !== $content && !is_numeric($content) && !is_string($content) && !is_callable([$content , '__toString'])){
                throw new \Exception("输出数据类型错误！");
            }else{
                $this->content = (string)$content;
            }
        }
        return $this->content;
    }

    /**
     * 返回输出数据
     */
    protected function output($data){
        return $data;
    }

    /**
     * 设置code
     */
    public function code($code){
        $this->code = $code;
        return $this;
    }

    /**
     * 页面缓存控制
     * @param string $cache 状态码
     * @return $this
     */
    public function cacheControl($cache)
    {
        $this->header['Cache-control'] = $cache;
        return $this;
    }

    /**
     * Expires
     * @param string $time
     * @return $this
     */
    public function expires($time)
    {
        $this->header['Expires'] = $time;
        return $this;
    }

    /**
     * 设置响应头
     * @access public
     * @param string|array $name  参数名
     * @param string       $value 参数值
     * @return $this
     */
    public function header($name, $value = null)
    {
        if (is_array($name)) {
            $this->header = array_merge($this->header, $name);
        } else {
            $this->header[$name] = $value;
        }
        return $this;
    }
}

